package com.wangzhenyu.learn

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.wangzhenyu.common.base.BaseVMFragment
import com.wangzhenyu.common.util.log.LogUtil
import com.wangzhenyu.learn.databinding.FragmentMainBinding
import com.wangzhenyu.learn.test.adapter.OnSellListAdapter


//这里使用Databinding
class MainFragment : BaseVMFragment<MainViewModel>() {

    private lateinit var binding: FragmentMainBinding

    override fun getSubVmClass(): Class<MainViewModel> {
        return MainViewModel::class.java
    }

    private val mAdapter by lazy {
        OnSellListAdapter()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false)

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initObserve()

        loadData()

        initView()

    }

    //监听数据
    private fun initObserve() {

        viewModel.apply {
            loadState.observe(viewLifecycleOwner)
            {
                LogUtil.default01(it.name)
            }

            contentList.observe(viewLifecycleOwner)
            {
                mAdapter.setData(it)
            }

            supportType.observe(viewLifecycleOwner)
            {
                if (it == ShowType.No) {
                    //不支持
                    binding.no.viewStub?.inflate()
                } else {
                    //支持
                    binding.yes.viewStub?.inflate()
                }
            }

            viewStubValue.observe(viewLifecycleOwner)
            {
                LogUtil.default03(it.toString())
                binding.viewStubValue = it
            }
        }
    }

    //加载数据
    private fun loadData() {

       // viewModel.loadData()
    }

    //初始化View
    private fun initView() {
        binding.recyclerView.run {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

}


